package com.token.model.dto;

import com.token.service.Supplier;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @auther: henriy
 * @Date: 2023/12/29
 */
public class ListDeduplicator<T> {

    private final Supplier<List<T>> listSupplier;

    public ListDeduplicator(Supplier<List<T>> listSupplier) {
        this.listSupplier = listSupplier;
    }

    public List<T> getDeduplicatedList() {
        return listSupplier.get()
                .stream()
                .distinct()
                .collect(Collectors.toList());
    }

    public static void main(String[] args) {
        List<String> listWithDuplicates = Arrays.asList("A", "B", "A", "C", "B", "D");

        ListDeduplicator<String> listDeduplicator = new ListDeduplicator<>(() -> listWithDuplicates);
        List<String> listWithoutDuplicates = listDeduplicator.getDeduplicatedList();
        // 输出: [A, B, C, D]
        System.out.println(listWithoutDuplicates);
    }

}
